/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * clientApp.java
 *
 * Created on Aug 22, 2013, 10:05:22 AM
 */

package cs137_chatapp;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author GON
 */
public class clientApp extends javax.swing.JFrame implements ActionListener, Runnable {

    /** Creates new form clientApp */
    public clientApp() {
        
    }

    private clientApp(String name, String ip, int port) {
        super(name);
        initComponents();
        setLocationRelativeTo(null);

        refreshButton.addActionListener((ActionListener) this);
        try{
		socket = new Socket(ip,port);
		br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                
                Date det = new Date();
		DateFormat dateF = new SimpleDateFormat("MM/dd/yyyy");
		DateFormat timeF = new SimpleDateFormat("HH:mm:ss");

                bw.write(name);bw.newLine(); bw.flush();
		String date = ("\n"+name+ " has logged in at "+dateF.format(det)+" "+timeF.format(det));
		bw.write(date); 
                bw.newLine();
                bw.flush();
                chatPanel.append(date);
		Thread th;
		th = new Thread(this);
		th.start();
	}catch(Exception e){}
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {//GEN-BEGIN:initComponents

        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        chatPanel = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        messageArea = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        sendButton = new javax.swing.JButton();
        refreshButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        StatusText = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(204, 204, 255));

        jScrollPane2.setBackground(new java.awt.Color(204, 204, 255));
        jScrollPane2.setEnabled(false);
        jScrollPane2.setFont(new java.awt.Font("Segoe UI Light", 0, 12));

        chatPanel.setFocusable(false);
        jScrollPane2.setViewportView(chatPanel);

        messageArea.setColumns(20);
        messageArea.setRows(3);
        jScrollPane3.setViewportView(messageArea);

        jLabel1.setText("Enter your message here:");

        sendButton.setFont(new java.awt.Font("Segoe UI Light", 1, 12)); // NOI18N
        sendButton.setText("Send");
        sendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendButtonActionPerformed(evt);
            }
        });

        refreshButton.setFont(new java.awt.Font("Segoe UI Light", 1, 12)); // NOI18N
        refreshButton.setText("Refresh");
        refreshButton.setEnabled(false);

        jPanel2.setBackground(new java.awt.Color(0, 0, 0));

        StatusText.setFont(new java.awt.Font("Segoe UI Light", 0, 12));
        StatusText.setForeground(new java.awt.Color(255, 255, 255));
        StatusText.setText("Status: Syncing Attachments");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(StatusText, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(StatusText, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 335, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(7, 7, 7)
                        .addComponent(sendButton, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE))
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 421, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(refreshButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane3, 0, 0, Short.MAX_VALUE)
                    .addComponent(sendButton, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(refreshButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }//GEN-END:initComponents

    private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendButtonActionPerformed
        try {
//            System.out.println(messageArea.getText());
            System.out.println(name+": "+messageArea.getText());
            bw.write(name+": "+messageArea.getText());
            bw.newLine();
            bw.flush();
            chatPanel.append("You: "+messageArea.getText()+"\n");
            messageArea.setText(" ");
        } catch (IOException ex) {
            Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_sendButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    static String name;
    public static void main(String args[]) {
        System.out.println("Client;");
        int port;
        String ip;

        //name = JOptionPane.showInputDialog(null, "Enter Chat Name: ");
        //ip = JOptionPane.showInputDialog(null, "Enter IP Address: ");
        //port = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Port Number: "));
        name = "gon";
        ip = "127.0.0.1";
        port = 12345;
        clientApp app = new clientApp(name,ip,port);
        app.setVisible(true);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel StatusText;
    private javax.swing.JTextArea chatPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea messageArea;
    private javax.swing.JButton refreshButton;
    private javax.swing.JButton sendButton;
    // End of variables declaration//GEN-END:variables

    public void actionPerformed(ActionEvent e) {
        Date det = new Date();
	DateFormat dateF = new SimpleDateFormat("MM/dd/yyyy");
	DateFormat timeF = new SimpleDateFormat("HH:mm:ss");

        if(e.getSource()==sendButton) {
//		enterSend();
        }
    }

    /*
     private void enterSend(){
	sendMsgToSomeone(messageArea.getText());
	chatPanel.append(messageArea.getText()+"\n");
	messageArea.setText("");
    }

    public void sendMsgToSomeone(String msge){
	try{
		bw.write("33xx1");
		bw.newLine(); bw.flush();
		bw.write(msge);
		bw.newLine(); bw.flush();
		bw.write("xx551");
		bw.newLine(); bw.flush();
	} catch(Exception kfg){}
    }
*/
    BufferedReader br;
    BufferedWriter bw;
    Socket socket;
    
    public void run() {
        String msgg="";
	while (true){
		try{
                    msgg = br.readLine();
                    System.out.println(msgg);
                    chatPanel.append("Server: "+msgg+"\n");
		}catch (Exception h){}
	}

    }

}
